package com.example.myworkers.controller;

import com.example.myworkers.model.Emp;
import com.example.myworkers.model.EmpClock;
import com.example.myworkers.service.EmpClockService;
import com.example.myworkers.service.EmpService;
import com.example.myworkers.utils.GetIpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;


@RestController
@RequestMapping("clock")
public class EmpClockController {

    @Autowired
    private EmpClockService empClockService;

    @Autowired
    private EmpService empService;

    /**
     * 打卡接口
     * @param servletRequest --
     * @param empClock empno-员工编号，tag--标记当日打卡
     * @return
     */
    @GetMapping("eClock")
    public String eClock(HttpServletRequest servletRequest, EmpClock empClock) {
        // 获取ip
        String ip = GetIpUtils.getRequesterIP(servletRequest);
        // 登录时间存入的是打卡者的ip
        empClock.setLogintime(ip);
        return empClockService.eClock(empClock);//ip，empno，tag
    }

//    @GetMapping("eClockEnd")
    public String eClockEnd(HttpServletRequest servletRequest, EmpClock empClock) {
        String ip = GetIpUtils.getRequesterIP(servletRequest);

        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        System.out.println(dateFormat.format(date));

        Emp emp = empService.getById(empClock.getEmpno());
        empClock.setLogintime(ip);
        empClock.setEmpno(empClock.getEmpno());
        empClock.setEmpname(emp.getEmpname());

        empClock.setEndtime(dateFormat.format(date));
        empClockService.save(empClock);
        return "打卡成功";
    }
}
